﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace Metier
{
    public static class GestionCuisinier
    {
        public static void Ajouter(Cuisinier cuisinier)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Utilisateurs.AddObject(cuisinier);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public static List<Cuisinier> Lister()
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Utilisateurs.OfType<Cuisinier>().ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static Cuisinier Chercher(PreparationPlat plat)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from c in db.Utilisateurs.OfType<Cuisinier>()
                            where c.PreparationPlat.Id == plat.Id
                            select c).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static Cuisinier Chercher(int cuisinierId)
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Utilisateurs.OfType<Cuisinier>().Where(c => c.Id == cuisinierId).FirstOrDefault();          
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<Cuisinier> Lister(Restaurant restaurant)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from c in db.Utilisateurs.OfType<Cuisinier>()
                            where c.Restaurant.Id == restaurant.Id
                            select c).ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
       
        public static Cuisinier Chercher(string email)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from c in db.Utilisateurs.OfType<Cuisinier>()
                            where c.Email == email
                            select c).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void MettreAJour(Cuisinier cuisinier)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Utilisateurs.Attach(cuisinier);
                    db.ObjectStateManager.ChangeObjectState(cuisinier, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void Supprimer(Cuisinier cuisinier)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Utilisateurs.DeleteObject(cuisinier);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
